package com.pinc.poop;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.ViewFlipper;

import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.AdView;

public class edit_poop extends PwdProtectWidgetActivity implements AdListener{
	private int id;
	private String timestr;
	private int color;
	private int shape;
	private int amount;
	private String note;
	private ImageView iv_color;
	private ImageView iv_shape;
	private DBClass dbHelper;
	private TimePicker time_pckr;
	public static final String[] colorStr={"#703020", "#7d7704", "#230000", "#969696", "#dec203", "#008000", "#ca0202"};
	public static final int[] shapeId={R.drawable.pp_banana, R.drawable.pp_thin, R.drawable.pp_icecream, R.drawable.pp_chestnut, R.drawable.pp_mud, R.drawable.pp_water};
	private ViewFlipper flipper;
	private LinearLayout[] mLayout;
	private static final int EDIT_PAGES = 2;
	private EditText et_note;
	private RatingBar rb_amount;
	private AdView adview;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_poop_parent);

		Intent intent = this.getIntent();
		Bundle bd= intent.getExtras();

		id = bd.getInt("db_id");

		dbHelper = new DBClass(this);
		Cursor cs = dbHelper.getRecord("_id = ?", new String[]{String.format("%d", id)}, null);
		try{
			if(cs.moveToFirst())
			{
				timestr = cs.getString(cs.getColumnIndex("time"));
				color = cs.getInt(cs.getColumnIndex("color"));
				shape = cs.getInt(cs.getColumnIndex("shape"));
				amount = cs.getInt(cs.getColumnIndex("amount"));
				note = cs.getString(cs.getColumnIndex("memo"));
			}
		} finally{
			cs.close();
			dbHelper.close();
		}
		cs.close();
		dbHelper.close();

		initWidget();
	}

	private void loadAd()
	{
		adview.setVisibility(View.GONE);
		adview.loadAd(new AdRequest());
	}

	@Override
	protected void onResume() {
		super.onResume();
		loadAd();
	}

	@Override
	protected void initWidget() {
		/* set upbar */
		TextView bt_back = (TextView) this.findViewById(R.id.button_left);
		bt_back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		TextView bt_more = (TextView) this.findViewById(R.id.button_right);
		bt_more.setVisibility(View.VISIBLE);
		bt_more.setText("... ");
		bt_more.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if ( flipper.getDisplayedChild() == 0)
					showNext();
				else
					showPrev();
			}
		});

		ImageView img_save = (ImageView) this.findViewById(R.id.imageView_save);
		img_save.setVisibility(View.VISIBLE);
		img_save.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dbHelper.updateRecord(id, String.format("%02d:%02d", time_pckr.getCurrentHour(), time_pckr.getCurrentMinute()), color, shape, (int)rb_amount.getRating(), et_note.getText().toString());
				finish();
			}
		});

		TextView tv_title = (TextView) findViewById(R.id.textView_title);
		tv_title.setText(getResources().getString(R.string.str_title_edit));


		/* set view flipper */
		flipper = (ViewFlipper) findViewById(R.id.ViewFlipper01);
		mLayout = new LinearLayout[EDIT_PAGES];

		LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		mLayout[0] = (LinearLayout) inflater.inflate(R.layout.edit_poop_1, new LinearLayout(this));
		mLayout[1] = (LinearLayout) inflater.inflate(R.layout.edit_poop_2, new LinearLayout(this));

		flipper.addView(mLayout[0]);
		flipper.addView(mLayout[1]);

		/* set time picker */
		time_pckr = (TimePicker) mLayout[0].findViewById(R.id.timePicker1);
		time_pckr.setIs24HourView(true);
		String hm[] = timestr.split(":");
		int mHour = Integer.parseInt(hm[0]);
		int mMinute = Integer.parseInt(hm[1]);
		time_pckr.setCurrentHour(mHour);
		time_pckr.setCurrentMinute(mMinute);

		/* set color */
		iv_color = (ImageView) mLayout[0].findViewById(R.id.imageView_color);

		ImageView iv_del_color = (ImageView) mLayout[0].findViewById(R.id.imageView_del_color);
		iv_del_color.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				iv_color.setImageDrawable(null);
				color = 0;
			}
		});

		LinearLayout ll_color = (LinearLayout) mLayout[0].findViewById(R.id.linearLayout_color);

		ImageView[] iv_colors = new ImageView[colorStr.length];

		for( int i=0; i<iv_colors.length; i++)
		{
			iv_colors[i] = genColorView(i);
			ll_color.addView(iv_colors[i]);
		}

		if(color!=0)
		{
			iv_color.setImageDrawable(iv_colors[color-1].getDrawable());
		}

		/* set shape */
		iv_shape = (ImageView) mLayout[0].findViewById(R.id.imageView_shape);

		ImageView iv_del_shape = (ImageView) mLayout[0].findViewById(R.id.imageView_del_shape);
		iv_del_shape.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				iv_shape.setImageDrawable(null);
				shape = 0;
			}
		});

		LinearLayout[] ll_shape = new LinearLayout[2];
		ll_shape[0] = (LinearLayout) mLayout[0].findViewById(R.id.linearLayout_shape1);
		ll_shape[1] = (LinearLayout) mLayout[0].findViewById(R.id.linearLayout_shape2);

		image_button[] iv_shapes = new image_button[getResources().getStringArray(R.array.str_arr_shape).length];

		for( int i=0; i<iv_shapes.length; i++)
		{
			iv_shapes[i] = genShapeView(i);
			ll_shape[i/4].addView(iv_shapes[i]);
		}

		if(shape!=0)
		{
			iv_shape.setImageDrawable(iv_shapes[shape-1].getDrawable());
		}

		/* set note */
		et_note = (EditText) mLayout[1].findViewById(R.id.editText1);
		et_note.setText(note);
		ImageView iv_del_note = (ImageView) mLayout[1].findViewById(R.id.imageView_del_note);
		iv_del_note.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				et_note.setText("");
			}
		});

		/* set amount */
		rb_amount = (RatingBar)  mLayout[1].findViewById(R.id.ratingBar1);
		rb_amount.setRating((float)amount);

		adview = (AdView) this.findViewById(R.id.adView);
		adview.setAdListener(this);
	}

	private ImageView genColorView(int id)
	{
		float r = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics());
		int size = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 35, getResources().getDisplayMetrics());
		int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics());
		ShapeDrawable sd = new ShapeDrawable(new RoundRectShape(new float[]{r,r,r,r,r,r,r,r}, null, null));
		sd.setIntrinsicHeight(size);
		sd.setIntrinsicWidth(size);
		sd.getPaint().setColor(Color.parseColor(colorStr[id]));
		ImageView iv = new ImageView(this);
		iv.setPadding(padding, padding, padding, padding);
		iv.setImageDrawable(sd);
		iv.setOnClickListener(new ColorClickListener(id));
		return iv;
	}

	private class ColorClickListener implements View.OnClickListener
	{
		private int id;

		public ColorClickListener(int id) {
			this.id = id;
		}
		@Override
		public void onClick(View v) {
			iv_color.setImageDrawable(((ImageView)v).getDrawable());
			color = id+1;
		}
	}

	private image_button genShapeView(int id)
	{
		image_button ib = new image_button(this);
		ib.setContent(shapeId[id], getResources().getStringArray(R.array.str_arr_shape)[id]);
		ib.setTextSize(13);
		ib.setOnClickListener(new ShapeClickListener(id));
		return ib;
	}

	private class ShapeClickListener implements View.OnClickListener
	{
		private int id;

		public ShapeClickListener(int id) {
			this.id = id;
		}

		@Override
		public void onClick(View v) {
			iv_shape.setImageDrawable(((image_button)v).getDrawable());
			shape = id+1;
		}
	}

	private void showNext()
	{
		flipper.setInAnimation(AnimationUtils.loadAnimation(edit_poop.this, R.anim.left_in));
		flipper.setOutAnimation(AnimationUtils.loadAnimation(edit_poop.this, R.anim.left_out));
		flipper.showNext();
		loadAd();
	}

	protected void showPrev()
	{
		flipper.setInAnimation(AnimationUtils.loadAnimation(edit_poop.this, R.anim.right_in));
		flipper.setOutAnimation(AnimationUtils.loadAnimation(edit_poop.this, R.anim.right_out));
		flipper.showPrevious();
		loadAd();
	}

	@Override
	public void onDismissScreen(Ad arg0) {
	}

	@Override
	public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
	}

	@Override
	public void onLeaveApplication(Ad arg0) {
	}

	@Override
	public void onPresentScreen(Ad arg0) {
	}

	@Override
	public void onReceiveAd(Ad arg0) {
		adview.setVisibility(View.VISIBLE);
	}

}
